<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      autocomplete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">联想知识库管理后台</h3>
      </div>
      <div class="form-container">
        <p class="banner">用户登录</p>
        <div class="items">
          <el-form-item prop="username">
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="手机号"
              name="username"
              type="text"
              tabindex="1"
              autocomplete="off"
              class="form-item"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              ref="password"
              v-model="loginForm.password"
              placeholder="密码"
              name="password"
              type="text"
              tabindex="1"
              autocomplete="off"
              class="form-item"
            />
          </el-form-item>
          <el-form-item prop="code" class="code-wrapper">
            <el-input
              ref="code"
              v-model="loginForm.code"
              placeholder="验证码"
              name="code"
              type="text"
              tabindex="1"
              autocomplete="off"
              class="form-item"
            />
            <img src="https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png" class="code-img">
          </el-form-item>
          <el-button
            :loading="loading"
            class="login-button"
            type="primary"
            @click.native.prevent="handleLogin"
          >登录</el-button>
        </div>
      </div>
    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不小于6位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'admin',
        password: '111111',
        code: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        code: [{ required: true, trigger: 'blur', message: '请输入验证码' }]
      },
      loading: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

<style lang="scss" scoped>

.login-container {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: url(../../assets/login/bg.jpg) no-repeat;
  background-size: cover;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .title-container {
    position: relative;
    .title {
      font-size: 36px;
      font-weight: bold;
      color: #FFFFFF;
      line-height: 47px;
      margin-bottom: 15px;
      text-align: center;
    }
  }
  .form-container {
    width: 448px;
    background: #FFFFFF;
    border-radius: 5px;
    .banner {
      padding: 28px 0px 23px 50px;
      font-size: 20px;
      font-weight: bold;
      color: #222222;
      letter-spacing: 1px;
    }
    .items {
      padding: 30px 50px 60px 50px;
      border-top: 1px solid #E4E7ED;
      .code-img {
        width: 139px;
        height: 48px;
        margin-left: 10px;
        flex-shrink: 1;
      }
      .login-button {
        width: 100%;
        height: 48px;
        background: #2684FC;
        border-radius: 5px;
        margin-top: 50px;
        font-size: 16px;
        font-weight: bold;
        color: #FFFFFF;
      }
    }
  }
}
::v-deep .el-form-item {
  margin-bottom: 16px;
}
::v-deep .el-input__inner {
  width: 100%;
  height: 48px;
  background: #F0F0F0;
  border-radius: 5px;
}
::v-deep .el-form-item__content {
  display: flex;
  flex-direction: row;
}
</style>
